﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Application.SystemManager
{
    public class MaterialFormatApp
    {
        private MaterialFormatRepository _materialFormatRepository;
        private ProductRepository _productRepository;
        public MaterialFormatApp()
        {
            _materialFormatRepository = new MaterialFormatRepository();
            _productRepository = new ProductRepository();
        }

        /// <summary>
        /// 获取规格列表
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IEnumerable<MaterialFormat> GetListByPID(Guid pid)
        {
            return _materialFormatRepository.IQueryable(a => a.MaterialId == pid).AsQueryable().OrderBy(a => a.Format);
        }

        public MaterialFormat GetInfo(Guid id)
        {
            return _materialFormatRepository.FindEntity(a => a.Id == id);
        }

        public int Add(MaterialFormat inv)
        {
            MaterialFormat tmp = _materialFormatRepository.FindEntity(a => a.MaterialId == inv.MaterialId && a.Width == inv.Width && a.Length == inv.Length);
            if (tmp != null)
            {
                return -1; //已存在该规格
            }
            return _materialFormatRepository.Insert(inv);
        }

        public int Update(MaterialFormat inv)
        {
            return _materialFormatRepository.Update(inv);
        }

    }
}
